package com.graduate.cec.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.graduate.cec.common.JsonPage;
import com.graduate.cec.common.ResponseCode;
import com.graduate.cec.exception.CecServiceException;
import com.graduate.cec.mapper.NationDetailMapper;
import com.graduate.cec.mapper.NationMapper;
import com.graduate.cec.pojo.dto.NationDTO;
import com.graduate.cec.pojo.dto.NationSimpleDTO;
import com.graduate.cec.pojo.entity.Nation;
import com.graduate.cec.pojo.entity.NationDetail;
import com.graduate.cec.pojo.entity.NationDetailExample;
import com.graduate.cec.pojo.entity.NationExample;
import com.graduate.cec.pojo.vo.NationVO;
import com.graduate.cec.util.CopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class NationService {
    @Resource
    private NationMapper nationMapper;

    @Resource
    private NationDetailMapper nationDetailMapper;


    /****************        增       ******************************************************************/
    public void insertNation(NationDTO nationDTO){
        Nation nation = CopyUtil.copy(nationDTO,Nation.class);
        int i = nationMapper.insertSelective(nation);
        if (i == 1){
            return ;
        }else {
            throw new CecServiceException(ResponseCode.INSERT_INFO_ERROR,"新增失败!");
        }

    }


    /****************        删       ******************************************************************/
    public void deleteByKeys(Long id){
        int i = nationMapper.deleteByPrimaryKey(id);
        if (i != 0){
            return ;
        }else{
            throw new CecServiceException(ResponseCode.DELETE_INFO_ERROR,"删除失败!");
        }
    }







    /****************        改       ******************************************************************/
    public void updateByKey(NationDTO nationDTO){
        Nation nation = CopyUtil.copy(nationDTO, Nation.class);
        int i = nationMapper.updateByPrimaryKeySelective(nation);
        if (i == 1){
            return;
        }else{
            throw new CecServiceException(ResponseCode.UPDATE_INFO_ERROR,"修改失败");
        }
    }

    /****************        查       ******************************************************************/
    public JsonPage<Nation> getListByExample(NationSimpleDTO nationSimpleDTO){
        PageHelper.startPage(nationSimpleDTO.getPageNum(), nationSimpleDTO.getPageSize());


        NationExample nationExample = new NationExample();
        if(!("".equals(nationSimpleDTO.getName())) && nationSimpleDTO.getName() != null){
            NationExample.Criteria criteria = nationExample.createCriteria();
            criteria.andNameEqualTo(nationSimpleDTO.getName());
        }
        List<Nation> nations = nationMapper.selectByExampleWithBLOBs(nationExample);

        return JsonPage.restPage(new PageInfo<>(nations));
    }

    public List<Nation> getAllList(){
        List<Nation> nations = nationMapper.selectByExampleWithBLOBs(new NationExample());
        return nations;

    }



    public NationVO getNationById(Long id){
        Nation nation = nationMapper.selectByPrimaryKey(id);

        NationVO nationVO = CopyUtil.copy(nation, NationVO.class);


        NationDetailExample nationDetailExample = new NationDetailExample();
        NationDetailExample.Criteria criteria = nationDetailExample.createCriteria();
        criteria.andNationIdEqualTo(id);
        List<NationDetail> nationDetails = nationDetailMapper.selectByExampleWithBLOBs(nationDetailExample);

        nationVO.setContent(nationDetails.get(0).getContent());
        nationVO.setViewCount(nationDetails.get(0).getViewCount());
        return nationVO;

    }
}
